package com.xinsoft.controller.device;

import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.CusTestPointPageParam;
import com.xinsoft.entity.dto.CusWarnRuleDTO;
import com.xinsoft.entity.dto.CusWarnRulePageParam;
import com.xinsoft.entity.dto.SysClientDTO;
import com.xinsoft.entity.po.CusWarnRule;
import com.xinsoft.service.CusTestPointService;
import com.xinsoft.service.CusWarnRuleService;
import com.xinsoft.utils.ExcelUtils;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/cus/warnRule")
public class CusWarnRuleController {

    @Autowired
    private CusWarnRuleService cusCusWarnRuleService;

    @Autowired
    private CusTestPointService cusCusTestPointService;

    /**
     * 新增测点
     */
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> add(@RequestBody CusWarnRule cusWarnRule) {
        // 参数校验
        ValidatorUtils.validateEntity(cusWarnRule);
        return Response.succeed(cusCusWarnRuleService.addCusWarnRule(cusWarnRule));
    }

    /**
     * 更新
     */
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> edit(@RequestBody  CusWarnRule cusWarnRule) {
        // 参数校验
        ValidatorUtils.validateEntity(cusWarnRule);
        return Response.succeed(cusCusWarnRuleService.editCusWarnRule(cusWarnRule));
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(cusCusWarnRuleService.getCusWarnRule(id));
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        cusCusWarnRuleService.del(ids);
        return Response.succeed();
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody CusWarnRulePageParam cusWarnRulePageParam) {
        return Response.succeed(cusCusWarnRuleService.findPageListByParam(cusWarnRulePageParam));
    }


    /**
     * 不分页
     */
    @PostMapping("/list")
    public Response<?> list(@RequestBody CusWarnRulePageParam cusWarnRulePageParam) {
        return Response.succeed(cusCusWarnRuleService.findListByParam(cusWarnRulePageParam));
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public Response<?> export(@RequestBody @ApiParam("查询参数") CusWarnRulePageParam cusWarnRulePageParam, HttpServletResponse response) throws Exception {
        List<CusWarnRuleDTO> cusWarnRuleList = cusCusWarnRuleService.findListByParam(cusWarnRulePageParam);
        ExcelUtils.exportByEasypoi(cusWarnRuleList, SysClientDTO.class, "告警规则", "告警规则", response, "告警规则");
        return Response.succeed();
    }

    /**
     * 不分页
     */
    @PostMapping("/testPointListByRuleId")
    public Response<?> testPointListByRuleId(@RequestBody CusTestPointPageParam param) {
        return Response.succeed(cusCusTestPointService.testPointListByRuleId(param));
    }


}
